package com.jt.zcdemo.controller;

import cn.hutool.core.date.TimeInterval;
import com.alibaba.fastjson2.JSONObject;
import com.jt.zcdemo.order.OrderInfoServiceImpl;
import com.jt.zcdemo.student.mapper.UserInfoMapper;
import com.jt.zcdemo.student.pojo.entity.UserInfo;
import com.jt.zcdemo.student.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CountDownLatch;

@RestController
@RequestMapping("/demo")
@Slf4j
public class DemoTestController {

    @Resource
    private UserInfoMapper userInfoMapper;


    @Resource(name = "userService")
    private UserInfoService userInfoService;

    @Resource
    private OrderInfoServiceImpl orderInfoService;

    @GetMapping("/getOrderInfo")
    public String getOrderInfo(){
        orderInfoService.getOrderInfo();
        return "hello getOrderInfo";
    }

    @GetMapping("/insertOrder")
    public String insertOrder(){
        //采用多线程进行插入并计算执行时间
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 设置线程池参数
        executor.setCorePoolSize(20);  // 核心线程数
        executor.setMaxPoolSize(50);   // 最大线程数
        executor.setQueueCapacity(10000000);  // 队列容量
        executor.setThreadNamePrefix("myExecutor-");  // 线程名前缀
        executor.initialize();  // 初始化线程池

        CountDownLatch latch = new CountDownLatch(1000000); // 设置计数器为10，表示有10个任务需要完成

        TimeInterval timer = new TimeInterval();
        for (int i = 0; i < 1000000; i++) {
            executor.execute(() -> {
                orderInfoService.insert();
                latch.countDown();
            });
        }

        try {
            latch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        executor.shutdown();


        log.info("执行时间：{}", timer.intervalPretty());

        return "hello insertOrder";
    }


    @GetMapping("/message")
    public String getMessage(@RequestParam String msg){
        log.info("hello message :{}",msg);

        String db = "slave_1";

        List<UserInfo> list = userInfoService.getUserList(db);

        log.info("the list is:{}",JSONObject.toJSONString(list));

        return "hello " + msg;

    }

    @GetMapping("/insert")
    public String insert(){
        log.info("hello insert");

        log.info("the list is:{}", JSONObject.toJSONString(userInfoMapper.selectList(null)));

        UserInfo userInfo   = new UserInfo();

        userInfo.setId(12344L);

        userInfo.setUserName("zhangsan1");

        userInfo.setUserCode("123456");

        userInfoMapper.insert(userInfo);

        return "hello insert";

    }
}
